/**
 * @author attilax 老哇的爪子
	@since  2014-6-17 上午09:28:00$
 */
package com.attilax.token;

import java.io.File;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.attilax.io.pathx;
import com.attilax.ioc.IocUtilV2;
import com.attilax.lang.Global;
import com.attilax.net.cookieUtil;
import com.attilax.seo.AppIco;
import com.attilax.util.PropX;
import com.attilax.util.randomx;
import com.attilax.util.tryX;
//import com.focustar.interceptor.promise;














import m.secury.callbackItfs;

/** com.attilax.token.TokenServiceV2
 * @author attilax 老哇的爪子
 * @since 2014-6-17 上午09:28:00$
 * @version v3.fix   ... fix v2 d get uid algo
 */
public class TokenServiceV3 {

 

	String appId;
	private String utype;

	public String getUtype() {
		return utype;
	}

	public TokenServiceV3 setUtype(String utype) {
		this.utype = utype;
		return this;
	}

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午09:28:00$
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// attilax 老哇的爪子 上午09:28:00 2014-6-17
		com.attilax.token.TokenServiceV3 ts=com.attilax.ioc.IocUtilV2.getBean(TokenServiceV3.class);
		ts.setModule("merMod");
		//ts.clrToken(resp);
		System.out.println(System.getProperty("appId"));

	}

	public boolean isHasToken(HttpServletRequest req) {
		String uid = cookieUtil.getCookie(appId + "_uid_"+utype, req);
		if (uid.equals(""))
			return false;
		else
			return true;

	}

	// attilax 老哇的爪子 上午09:28:00 2014-6-17

 

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午09:37:37$
	 * 
	 * @param name
	 * @param id
	 * @param httpServletResponse
	 */
	@Deprecated
	public void setToken(final String name, final String uid,
			final HttpServletResponse httpServletResponse) {
		// attilax 老哇的爪子 上午09:37:37 2014-6-17
		new tryX<Object>() {

			@Override
			public Object item(Object t) throws Exception {
				// attilax 老哇的爪子 上午09:50:43 2014-6-17
				cookieUtil.add(appId + "_uname_"+utype+"Utype", name, httpServletResponse);
				cookieUtil.add(appId + "_uid_"+utype+"Utype", uid.toString(),
						httpServletResponse);
				// cookieUtil.add("uname",name,httpServletResponse);
				// cookieUtil.add(appId+"_uid",uid.toString(),httpServletResponse);
				return null;
			}
		}.$("");

	}
	
	public TokenServiceV3 setToken(final String name, final String uid
			) {
		final HttpServletResponse httpServletResponse=Global.resp.get();
		// attilax 老哇的爪子 上午09:37:37 2014-6-17
		new tryX<Object>() {

			@Override
			public Object item(Object t) throws Exception {
				// attilax 老哇的爪子 上午09:50:43 2014-6-17
				cookieUtil.add(appId + "_uname_"+utype+"Utype", name, httpServletResponse);
				cookieUtil.add(appId + "_uid_"+utype+"Utype", uid.toString(),
						httpServletResponse);
				// cookieUtil.add("uname",name,httpServletResponse);
				// cookieUtil.add(appId+"_uid",uid.toString(),httpServletResponse);
				return this;
			}
		}.$("");
	//	reutrn this;
		return this;

	}
	
	public TokenServiceV3 setTokenKeyVal(String name,String val)
	{
		final HttpServletResponse httpServletResponse=Global.resp.get();
		cookieUtil.add(appId + "_"+name+"_"+utype+"Utype", val, httpServletResponse);
	//	cookieUtil.add(appId + "_uid_"+utype+"Utype", uid.toString(),
		return this;
	}
	
	public Object getTokenKeyVal(String name )
	{
		final HttpServletResponse httpServletResponse=Global.resp.get();
		return	cookieUtil.getCookie(appId + "_"+name+"_"+utype+"Utype",  Global.req.get());
	//	cookieUtil.add(appId + "_uid_"+utype+"Utype", uid.toString(),
	//	return this;
	}


	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午09:57:08$
	 * 
	 * @param object
	 */
	public static void exTok(final com.attilax.promise object) {
		// attilax 老哇的爪子 上午09:57:08 2014-6-17
		new tryX<Object>() {

			@Override
			public Object item(Object t) throws Exception {
				// attilax 老哇的爪子 上午10:45:39 2014-6-17
				object.$();
				return null;
			}
		}.$("");

	}

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午10:13:13$
	 * 
	 * @return
	 */
	public   String getuid(HttpServletRequest req) {
		// attilax 老哇的爪子 上午10:13:13 2014-6-17
      
		  String utype=getUtype(req);
        if(this.utype==null)
        	this.utype=utype+"";
		String cookieName = appId+"_uid_"+this.utype+"Utyp";
		return cookieUtil.getCookie(cookieName, req);
	}

	/**
	attilax    2016年4月18日  下午10:50:12
	 * @param req
	 * @return
	 */
	private String getUtype(HttpServletRequest req) {
		String utype="";
		if(req==null)
			throw new RuntimeException(" req is null");
	if(	req.getParameter("$utype")!=null)
		utype=req.getParameter("$utype");
		return utype;
	}

	public   String getuname(HttpServletRequest request) {
		// attilax 老哇的爪子 上午10:13:13 2014-6-17

		return cookieUtil.getCookie(appId+"_uname_"+this.utype, request);
	}
	
	public   String getuname(HttpServletRequest request,String appid,String modId) {
		// attilax 老哇的爪子 上午10:13:13 2014-6-17

		return cookieUtil.getCookie(appid+"_uname_"+modId, request);
	}

	/**
	 * 
	 * @author attilax 老哇的爪子
	 * @since 2014-6-24 下午6:35:13$
	 */
	public void clrToken(final HttpServletResponse resp) {
		// attilax 老哇的爪子 下午6:35:13 2014-6-24
		new tryX<Object>() {

			@Override
			public Object item(Object t) throws Exception {
				// attilax 老哇的爪子 上午09:50:43 2014-6-17
				cookieUtil.add(appId + "_uname_"+utype, "", resp);
				cookieUtil.add(appId + "_uid_"+utype, "", resp);
				return null;
			}
		}.$("");

	}

//	/**
//	 * @return
//	 * @author attilax 老哇的爪子
//	 * @since 2014-6-24 下午6:47:38$
//	 */
//	public static boolean isLocal() {
//		// attilax 老哇的爪子 下午6:47:38 2014-6-24
//
//		return new com.attilax.tryX<Boolean>() {
//
//			@Override
//			public Boolean $$(Object t) throws Exception {
//				// attilax 老哇的爪子 下午6:48:34 2014-6-24
//				// if()
//				return new File("c:\\localFlag").exists();
//			}
//		}.$(false);
//	}
//
	@Deprecated
	public TokenServiceV3 setModule(String mod) {
	this.utype=mod;
	return this;
		
	}

	public String getuid() {
		 
		HttpServletRequest req = Global.req.get();
		if(req==null)
			throw new RuntimeException(" Global.req.get() is null");
		if(req.getParameter("$uid_cukname")!=null && req.getParameter("$uid_cukname").length()>0 )
		{
			String cookieName=req.getParameter("$uid_cukname").trim();
			return cookieUtil.getCookie(cookieName, req);
		}
			
		
		
		return getuid(req);
	}

	/**
	attilax    2016年6月18日  下午11:12:17
	 * @param string
	 */
	public void setUrole(String role) {
		setTokenKeyVal("urole",role);
		
	}
	
	public String getUrole(  ) {
	return	(String) getTokenKeyVal("urole" );
		
	}
}

// attilax 老哇的爪子